package com.liwq.test;

import org.openjdk.jol.info.ClassLayout;

import java.util.concurrent.TimeUnit;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2023/2/2
 **/

class ticket {


    private int num = 50;
    Object o = new Object();

    public void test() {
        synchronized (o) {
            if (num > 0) {
                num--;
                System.out.println(Thread.currentThread().getName() + "卖出第" + num + "");


            }

        }
    }


}

public class SynchorizedDemo {


    public static void main(String[] args) throws InterruptedException {

        ticket t = new ticket();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                t.test();
            }

        }).start();

        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                t.test();
            }

        }).start();


        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                t.test();
            }

        }).start();


    }


}
